<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima.png" alt="logo">
                <span>某某后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出登录</el-button>
        </el-header>
        <!-- 页面主体区 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapses?'64px':'200px'">
                <div class="toggle-button" @click="toggleCollapse">☞</div>
                <!-- 菜单区 -->
                <el-menu unique-opened background-color="#323744" text-color="#fff" active-text-color="#359aff"
                    :collapse='isCollapses' :collapse-transition="false" router :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu v-for="item in Menulist" :index="item.id+''" :key="item.id">
                        <!-- 一级菜单模板区 -->
                        <template slot="title">
                            <i :class="iconObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                            @click="saveNavState('/'+subItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{subItem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>

            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>

</template>

<script>
    export default {
        name: 'Hmoe',
        data() {
            return {
                Menulist: [],
                iconObj: {
                    '125': 'el-icon-user-solid',
                    '103': 'el-icon-s-grid',
                    '101': 'el-icon-s-goods',
                    '102': 'el-icon-s-order',
                    '145': 'el-icon-s-marketing'
                },
                isCollapses: false,
                activePath: ''
            }
        },
        created() {
            this.getMenulist()
            this.activePath = window.sessionStorage.getItem('activePath')

        },
        methods: {
            logout() {
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            // 获取所有菜单
            async getMenulist() {
                const { data: res } = await this.$http.get('menus')
                if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
                this.Menulist = res.data
                // console.log(res);
            },
            // 折叠
            toggleCollapse() {
                this.isCollapses = !this.isCollapses
            },
            // 保存链接的激活状态
            saveNavState(activePath) {
                window.sessionStorage.setItem('activePath', activePath)
                this.activePath = activePath
            }
        },
    }
</script>

<style scoped>
    .el-header {
        display: flex;
        justify-content: space-between;
        background-color: #373c41;
        padding-left: 0;
        align-items: center;
        color: #fff;
        font-size: 25px;
    }

    .el-header div {
        display: flex;
        align-items: center;
    }

    .el-header div span {
        margin-left: 30px;
    }

    .el-aside {
        background-color: #323744;
    }

    .el-aside .el-menu {
        border-right: 0;
    }

    .el-main {
        background-color: #eaedf2;
    }

    .home-container {
        height: 100%;
    }

    .toggle-button {
        background-color: #485066;
        color: #fff;
        font-size: 20px;
        line-height: 20px;
        text-align: center;
        cursor: pointer;
    }
</style>